import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/location/state.dart';
import 'package:industry_net/pages/msg/map_earth/state.dart';

class MapEarthController extends GetxController {
  final state = MapEarthState();
  @override
  void onInit() async {
    super.onInit();
    state.locationCtrl.state.mapType = MapType.satellite;
    state.map = await state.locationCtrl.createMap();
    update();
    state.locationCtrl.paintCallback = () async {
      state.map = await state.locationCtrl.createMap();
      update();
    };
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  void backToOrigin() {
    state.locationCtrl.backOrigin();
    // state.locationCtrl.state.mapCtrl?.moveCamera(
    //     CameraUpdate.newLatLng(state.locationCtrl.md.value.position));
  }

  void switchMapType() async {
    if (state.locationCtrl.state.mapType == MapType.normal) {
      state.locationCtrl.state.mapType = MapType.satellite;
    } else {
      state.locationCtrl.state.mapType = MapType.normal;
    }
    updatePaint();
  }

  void updatePaint() async {
    state.map = await state.locationCtrl.createMap();
    update();
  }

  void actionPainting() {
    state.locationCtrl.state.paintState.value = MapPaintState.painting;
    state.locationCtrl.state.points = [];
    state.locationCtrl.paint();
    updatePaint();
  }

  void actionResetPaint() {
    state.locationCtrl.state.paintState.value = MapPaintState.painting;
    state.locationCtrl.state.points = [];
    state.locationCtrl.paint();
    updatePaint();
  }

  void actionRevokePaint() {
    if (state.locationCtrl.state.points.isEmpty) {
      return;
    }
    state.locationCtrl.state.paintState.value = MapPaintState.painting;
    state.locationCtrl.state.points.removeLast();
    state.locationCtrl.paint();
    updatePaint();
  }

  void actionCancelPaint() {
    state.locationCtrl.state.paintState.value = MapPaintState.idle;
    if (state.locationCtrl.state.points.isEmpty) {
      update();
      return;
    }
    state.locationCtrl.state.points = [];
    state.locationCtrl.paint();
    updatePaint();
  }

  void actionFinishPaint() {
    state.locationCtrl.state.paintState.value = MapPaintState.idle;
    state.locationCtrl.state.points = [];
    state.locationCtrl.paint();
    updatePaint();
  }

  @override
  void onClose() {
    state.locationCtrl.state.mapCtrl?.disponse();
    state.locationCtrl.location.destroy();
    super.onClose();
  }

  @override
  void dispose() {
    super.dispose();
  }
}
